08. Gradients and Sobel Filters

Gradients

Gradients are a measure of intensity change in an image, and they generally mark object boundaries and changing area of light and dark. If we think back to treating images as functions, F(x, y), we can think of the gradient as a derivative operation F (x, y). Where the derivative is a measurement of intensity change.

Sobel filters

The Sobel filter is very commonly used in edge detection and in finding patterns in intensity in an image. Applying a Sobel filter to an image is a way of taking (an approximation) of the derivative of the image in the x or y direction. The operators for Sobel_x and Sobel_y, respectively, look like this:

Sobel filters

Sobel filters

Next, let's see an example of these two filters applied to an image of the brain.

Sobel x and y filters (left and right) applied to an image of a brain

Sobel x and y filters (left and right) applied to an image of a brain

x vs. y

In the above images, you can see that the gradients taken in both the x and the y directions detect the edges of the brain and pick up other edges. Taking the gradient in the x direction emphasizes edges closer to vertical. Alternatively, taking the gradient in the y direction emphasizes edges closer to horizontal.

Magnitude

Sobel also detects which edges are strongest. This is encapsulated by the magnitude of the gradient; the greater the magnitude, the stronger the edge is. The magnitude, or absolute value, of the gradient is just the square root of the squares of the individual x and y gradients. For a gradient in both the x and y directions, the magnitude is the square root of the sum of the squares.

abs_sobelx = \sqrt{(sobel_x)^2}

abs_sobely = \sqrt{(sobel_y)^2}

abs_sobelxy = \sqrt{(sobel_x)^2+(sobel_y)^2}

Direction

In many cases, it will be useful to look for edges in a particular orientation. For example, we may want to find lines that only angle upwards or point left. By calculating the direction of the image gradient in the x and y directions separately, we can determine the direction of that gradient!

The direction of the gradient is simply the inverse tangent (arctangent) of the y gradient divided by the x gradient:

tan^{-1}{(sobel_y/sobel_x)}.